﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.SessionState;

namespace RC1.admin
{
    public partial class edit_class : System.Web.UI.Page
    {
        int class_id;

        protected void Page_Load(object sender, EventArgs e)
        {
            class_id = Convert.ToInt32(Request.QueryString["id"]);
            if (!Page.IsPostBack)
            {
                populatePage();
            }
        }

        private void populatePage()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var myClass = (from c in db.classes
                           where c.class_id == class_id
                           select c).SingleOrDefault();
            
            classNameTextBox.Text = myClass.class_name;
            descriptionTextBox.Text = myClass.class_description;

            var results = (from s in db.subjects
                           select s);
            subjectDropDownList.DataSource = db.subjects;
            subjectDropDownList.DataTextField = "subject_name";
            subjectDropDownList.DataValueField = "subject_id";
            subjectDropDownList.DataBind();

            int indexOfSubject = subjectDropDownList.Items.IndexOf(subjectDropDownList.Items.FindByValue(myClass.subject_id.ToString()));
            subjectDropDownList.SelectedIndex = indexOfSubject;

            var teacherResults = (from u in db.lpusers
                                  join ui in db.user_info on u.user_id equals ui.user_id
                                  where u.user_level == 1
                                  select new
                                  {
                                      user_id = u.user_id,
                                      name = ui.user_info_first_name + " " + ui.user_info_last_name
                                  });
            teacherDropDownList.DataSource = teacherResults;
            teacherDropDownList.DataTextField = "name";
            teacherDropDownList.DataValueField = "user_id";
            teacherDropDownList.DataBind();

            int indexOfTeacher = teacherDropDownList.Items.IndexOf(teacherDropDownList.Items.FindByValue(myClass.user_id.ToString()));
            teacherDropDownList.SelectedIndex = indexOfTeacher;
        }

        private void updateClass()
        {
            tsu2011db4Entities1 db = new tsu2011db4Entities1();
            var cl = (from c in db.classes
                           where c.class_id == class_id
                           select c).SingleOrDefault();

            String className = classNameTextBox.Text;
            String description = descriptionTextBox.Text;
            int subjectID = Convert.ToInt32(subjectDropDownList.SelectedValue);
            int teacherID = Convert.ToInt32(teacherDropDownList.SelectedValue);

            cl.class_description = description;
            cl.class_name = className;
            cl.subject_id = subjectID;
            cl.user_id = teacherID;
            db.SaveChanges();
            goBack();
        }

        private void goBack()
        {
            if (Request.QueryString["p"] != null)
            {
                Response.Redirect("delete_user.aspx?id=" + Request.QueryString["p"]);
            }
            else
            {
                Response.Redirect("class.aspx");
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            updateClass();
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            goBack();
        }
    }
}